/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"

#include "OBJ模型加载保存.h"
#include "面/面.h"

#include <文件/file_载入保存数据.h>
#include <字符串/str_字符串读写.h>



static void f_读顶点(std::vector<std::string>& 行信息, std::vector<vec3>& vert) {
	vec3 v;
	v.x = atof(行信息[0].c_str());
	v.y = atof(行信息[1].c_str());
	v.z = atof(行信息[2].c_str());
	vert.emplace_back(v);
}

static void f_读纹理坐标(std::vector<std::string>& 行信息, std::vector<vec2>& uv) {
	vec2 v;
	v.x = atof(行信息[0].c_str());
	v.y = atof(行信息[1].c_str());
	uv.emplace_back(v);
}

static void f_读索引(std::vector<std::string>& 行信息, std::vector<uint32>& index) {
	uint32 num = 行信息.size();
	for (uint32 i = 0; i < num; ++i) {
		std::vector<std::string> 索引;
		f_str_分割(索引, 行信息[i], '/');

		for (auto& e : 索引) {
			index.emplace_back(atoi(e.c_str()));
		}
	}
}

static void f_分离索引(std::vector<uint32>& index, std::vector<uint32>& v_index, std::vector<uint32>& uv_index) {
	uint32 num = index.size() / 2;
	auto index_p = index.data();

	v_index.resize(num);
	uv_index.resize(num);
	auto vi_p = v_index.data();
	auto ui_p = uv_index.data();
	for (uint32 i = 0; i < num; ++i) {
		vi_p[i] = index_p[i * 2];
		ui_p[i] = index_p[i * 2 + 1];
	}
}


S_物体* f_Ex加载OBJ模型(const std::string& path, S_设备环境& ctx) {
	S_物体* ob = new S_物体(E_物体类型::t_网格);
	f_ob_创建矩阵(ob, ctx);
	f_ob_set数据槽数量(ob, 1);

	FILE* f = fopen(path.c_str(), "rb");
	if (f) {
		S_Mesh* me = f_surface_创建网格对象(ctx, E_网格数据枚举::e_Mesh_VULKAN);

		std::string code = f_读取字符串(f);

		uint32 num = code.size();
		auto codePtr = code.data();
		//auto codePtr = code.c_str();
		std::vector<vec3> vert;
		std::vector<vec3> normal;
		std::vector<vec2> uv;
		std::vector<uint32> index;


		for (uint32 i = 0; *codePtr != '\0'; ) {
			if (*codePtr == '#' || *codePtr == '\r') {
				++codePtr;
				codePtr = f_file_到下一行(codePtr);
				continue;
			}
			else {
				std::string 一行;
				codePtr = f_str_读一行(codePtr, 一行);

				
				if (一行.size()) {
					std::vector<std::string> strArray;
					f_str_分割(strArray, 一行, ' ');
					//std::cout << "一行 = " << 一行 << std::endl;
					auto h = strArray.front();
					strArray.erase(strArray.begin());
					if (h == "v") {
						f_读顶点(strArray, vert);
					}
					else if (h == "vn") {
						f_读顶点(strArray, normal);
					}
					else if (h == "vt") {
						f_读纹理坐标(strArray, uv);
					}
					else if (h == "f") {
						f_读索引(strArray, index);
					}
					else if (h == "usematl") {

					}
				}
				
				continue;
			}

			++i;
			
		}

		fclose(f);

		uint32 flage = 0;
		if (vert.size()) flage |= 1 << 0;
		if (normal.size()) flage |= 1 << 1;
		if (uv.size()) flage |= 1 << 2;
		
		std::vector<S_VNT1> vnt;
		
		switch (flage) {
		case 1:
			f_surface_fill索引数据(me, index);
			break;
		case 3:
			break;
		case 5: {
			f_surface_数组合并(vnt, vert, uv);
			f_surface_fill顶点数据(me, vnt);

			std::vector<uint32> v_index;
			std::vector<uint32> u_index;
			f_分离索引(index, v_index, u_index);
			f_surface_fill索引数据(me, v_index);
			f_surface_fillUV索引数据(me, u_index.data(), u_index.size());
			break;
		}
		default:
			break;
		}

		f_ob_set数据(ob, me);
	}
	return ob;
}







